# -*- coding: utf-8 -*-
import os
import re
#import chardet

DEFAULT_IP_POOL = ['192.168.13.98', '192.168.13.99', '10.3.0.59']
SERVICE_IP = '192.168.13.100'

def get_network():
    ip_config_lines = os.popen('netsh interface ipv4 show address').readlines()
    intf = {}
    for cfg_line in ip_config_lines:
        cfg_line = cfg_line.strip().decode('gb2312')
        #print [cfg_line]
        if cfg_line == '' and intf and intf.get('ip') and intf.get('mask'):
            if set(intf.get('ip')).intersection(set(DEFAULT_IP_POOL)):
                return intf
            else:
                intf = {}
                continue
        m = re.match(u'接口 "([^"]+)" 的配置', cfg_line)
        if m:
            #print m.group(1)
            intf['name'] = m.group(1)
            continue
        m = re.match(u'IP 地址:\s*([\d\.]+)', cfg_line)
        if m:
            #print m.group(1)
            if not intf.get('ip'):
                intf['ip'] = []
            intf['ip'].append(m.group(1))
            continue
        m = re.match(u'.*\(掩码 ([\d\.]+)\)', cfg_line)
        if m:
            #print m.group(1)
            if not intf.get('mask'):
                intf['mask'] = []
            intf['mask'].append(m.group(1))
            continue
    return None

def set_network():
    cmdline = 'netsh interface ipv4 add address "" 192.168.13.100 255.255.255.0' % ("2内网",)
    ret = os.popen(cmdline).read()
    print [cmdline, ret]
if __name__ == '__main__':
    #print 'get_network:', get_network()
    set_network()